package org.javaforever.gatescore.vue;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.javaforever.gatescore.core.FrontDomain;
import org.javaforever.gatescore.core.FrontManyToMany;
import org.javaforever.gatescore.core.Statement;
import org.javaforever.gatescore.core.StatementList;
import org.javaforever.gatescore.core.Writeable;
import org.javaforever.gatescore.core.WriteableUtil;
import org.javaforever.gatescore.reports.LayoutComb;
import org.javaforever.gatescore.reports.ReportComb;
import org.javaforever.gatescore.utils.StringUtil;

public class VueRouter implements Comparable<VueRouter>,Cloneable,Serializable{
	private static final long serialVersionUID = -2942380430997684889L;
	protected String standardName = "Pages";
	protected List<FrontDomain> domains = new ArrayList<FrontDomain>();
	protected Set<FrontManyToMany> mtms = new TreeSet<FrontManyToMany>();
	protected List<ReportComb> reportCombs = new ArrayList<ReportComb>();
	protected List<LayoutComb> layoutCombs = new ArrayList<LayoutComb>();
	protected FrontDomain userDomain;
	public FrontDomain getUserDomain() {
		return userDomain;
	}
	
	public void setUserDomain(FrontDomain userDomain) {
		this.userDomain = userDomain;
	}
	
	@Override
	public int compareTo(VueRouter o) {
		return this.standardName.compareTo(o.getStandardName());
	}
	
	public String getStandardName() {
		return standardName;
	}
	
	public void setStandardName(String standardName) {
		this.standardName = standardName;
	}
	
	public String getFileName() {
		return StringUtil.lowerFirst(this.standardName)+".js";
	}
	
	public Set<FrontManyToMany> getMtms() {
		return mtms;
	}
	
	public void setMtms(Set<FrontManyToMany> mtms) {
		this.mtms = mtms;
	}
	
	public void addMtms(Set<FrontManyToMany> mtms) {
		this.mtms.addAll(mtms);
	}
	
	public StatementList generateStatementList() throws Exception{
		List<Writeable> sList = new ArrayList<Writeable>();
		sList.add(new Statement(1000L,0,"/** When your routing table is too long, you can split it into small modules**/"));
		sList.add(new Statement(2000L,0,""));
		sList.add(new Statement(3000L,0,"import Layout from '@/views/layout/Layout'"));
		sList.add(new Statement(4000L,0,""));
		sList.add(new Statement(5000L,0,"const pagesRouter = {"));
		sList.add(new Statement(6000L,1,"path: '',"));
		sList.add(new Statement(7000L,1,"component: Layout,"));
		sList.add(new Statement(8000L,1,"redirect: 'noredirect',"));
		sList.add(new Statement(9000L,1,"name: 'Pages',"));
		sList.add(new Statement(10000L,1,"meta: {"));
		sList.add(new Statement(11000L,2,"title: 'pages',"));
		sList.add(new Statement(12000L,2,"icon: 'component'"));
		sList.add(new Statement(13000L,1,"},"));
		sList.add(new Statement(14000L,1,"children: ["));
		
		String language = this.domains.get(0).getLanguage();
		String indexName = "主页";
		if ("english".equalsIgnoreCase(language)) indexName = "Home page";
		
		sList.add(new Statement(15000L,2,"{"));
		sList.add(new Statement(16000L,3,"path: 'index',"));
		sList.add(new Statement(17000L,3,"component: () => import('@/views/pages/index'),"));
		sList.add(new Statement(18000L,3,"name: 'Index',"));
		sList.add(new Statement(19000L,3,"meta: { title: '"+indexName+"', noCache: true }"));
		sList.add(new Statement(20000L,2,"},"));

		long serial = 21000L;
		for (FrontDomain d:this.domains) {
			sList.add(new Statement(serial,2,"{"));
			sList.add(new Statement(serial+1000L,3,"path: '"+d.getPlural().toLowerCase()+"',"));
			sList.add(new Statement(serial+2000L,3,"component: () => import('@/views/pages/"+d.getPlural().toLowerCase()+"'),"));
			sList.add(new Statement(serial+3000L,3,"name: '"+d.getStandardName()+"',"));
			sList.add(new Statement(serial+4000L,3,"meta: { title: '"+d.getText()+"', noCache: true }"));
			sList.add(new Statement(serial+5000L,2,"},"));
			serial += 6000L;
		}

		if (this.userDomain!=null) {
			sList.add(new Statement(serial,2,"{"));
			sList.add(new Statement(serial+1000L,3,"path: 'profile',"));
			sList.add(new Statement(serial+2000L,3,"component: () => import('@/views/pages/profile'),"));
			sList.add(new Statement(serial+3000L,3,"name: 'Profile',"));
			if ("english".equalsIgnoreCase(language)) {
				sList.add(new Statement(serial+4000L,3,"meta: { title: '"+this.userDomain.getText()+" Profile', noCache: true }"));
			} else {
				sList.add(new Statement(serial+4000L,3,"meta: { title: '"+this.userDomain.getText()+"资料', noCache: true }"));
			}
			sList.add(new Statement(serial+5000L,2,"},"));
			serial += 6000L;
		}
		
		for (FrontManyToMany mtm:this.mtms) {
			sList.add(new Statement(serial,2,"{"));
			sList.add(new Statement(serial+1000L,3,"path: '"+mtm.getStandardName().toLowerCase()+"',"));
			sList.add(new Statement(serial+2000L,3,"component: () => import('@/views/pages/"+mtm.getStandardName().toLowerCase()+"'),"));
			sList.add(new Statement(serial+3000L,3,"name: '"+mtm.getStandardName()+"',"));
			sList.add(new Statement(serial+4000L,3,"meta: { title: '"+mtm.getText()+"', noCache: true }"));
			sList.add(new Statement(serial+5000L,2,"},"));
			serial += 6000L;
		}
		
		for (LayoutComb lc:this.layoutCombs) {
			sList.add(new Statement(serial,2,"{"));
			sList.add(new Statement(serial+1000L,3,"path: '"+lc.getStandardName().toLowerCase()+"',"));
			sList.add(new Statement(serial+2000L,3,"component: () => import('@/views/pages/"+lc.getStandardName().toLowerCase()+"'),"));
			sList.add(new Statement(serial+3000L,3,"name: '"+lc.getStandardName()+"',"));
			sList.add(new Statement(serial+4000L,3,"meta: { title: '"+lc.getText()+"', noCache: true }"));
			sList.add(new Statement(serial+5000L,2,"},"));
			serial += 6000L;
		}
		
		for (ReportComb rc:this.reportCombs) {
			sList.add(new Statement(serial,2,"{"));
			sList.add(new Statement(serial+1000L,3,"path: '"+rc.getStandardName().toLowerCase()+"',"));
			sList.add(new Statement(serial+2000L,3,"component: () => import('@/views/pages/"+rc.getStandardName().toLowerCase()+"'),"));
			sList.add(new Statement(serial+3000L,3,"name: '"+rc.getStandardName()+"',"));
			sList.add(new Statement(serial+4000L,3,"meta: { title: '"+rc.getText()+"', noCache: true }"));
			sList.add(new Statement(serial+5000L,2,"},"));
			serial += 6000L;
		}

		sList.add(new Statement(serial,1,"]"));
		sList.add(new Statement(serial+1000L,0,"}"));
		sList.add(new Statement(serial+2000L,0,""));
		sList.add(new Statement(serial+3000L,0,"export default pagesRouter"));
		return WriteableUtil.merge(sList);
	}
	
	public List<FrontDomain> getDomains() {
		return domains;
	}
	
	public void setDomains(List<FrontDomain> domains) {
		this.domains = domains;
	}
	
	public List<ReportComb> getReportCombs() {
		return reportCombs;
	}
	
	public void setReportCombs(List<ReportComb> reportCombs) {
		this.reportCombs = reportCombs;
	}
	
	public List<LayoutComb> getLayoutCombs() {
		return layoutCombs;
	}
	
	public void setLayoutCombs(List<LayoutComb> layoutCombs) {
		this.layoutCombs = layoutCombs;
	}
}
